package com.hnqcgc.db;

import java.sql.Connection;
import java.sql.SQLException;

public class MyTransaction {
    private Connection connection;
  public MyTransaction(boolean isAuto){
      try {
          this.connection =new DB().getConn();
          this.connection.setAutoCommit(isAuto);
      } catch (SQLException throwables) {
          throwables.printStackTrace();
      }
  }
  //手动提交方法
    public void commit(){
      if (connection!=null){
          try {
              connection.commit();
          } catch (SQLException throwables) {
              throwables.printStackTrace();
          }
      }
    }

  //手动回滚方法
  public void rollback(){
      if (connection!=null){
          try {
              connection.rollback();
          } catch (SQLException throwables) {
              throwables.printStackTrace();
          }
      }
  }
  //获得数据库连接的方法
    public Connection getConnection(){
      return this.connection;
    }
  //关闭数据库连接
  public void closeConn(){
      try {
          if (connection!=null&&!connection.isClosed()){
              connection.close();
          }
      } catch (SQLException throwables) {
          throwables.printStackTrace();
      }
  }
}
